"use strict";

var dialog = new Object();
dialog.dom = {};
dialog.default = function(json) {
    if(Object.assign){
        return Object.assign({}, {
            id: "dmui-dialog",
            width: "",
            height: "",
            zIndex: "",
            header: "",
            body: "",
            ajax: "",
            footer: "",
            opacity: false,
            callback: null
        }, json);
    }
    else{
        return jQuery.extend(true, {}, {
            id: "dmui-dialog",
            width: "",
            height: "",
            zIndex: "",
            header: "",
            body: "",
            ajax: "",
            footer: "",
            opacity: false,
            callback: null
        }, json);
    }
};
dialog.add = function() {
    $("body").append(['<div id="J_', dialog.json.id, '" class="dmui-dialog show" data-close=""><div class="dmui-dialog-box">', '<div class="dmui-dialog-header"></div>', '<div class="dmui-dialog-body"></div>', '<div class="dmui-dialog-footer"></div>', '</div></div>'].join(""));
    dialog.edit();
    dialog.event();
};
dialog.readDom = function() {
    dialog.dom.root = $("#J_" + dialog.json.id);
    dialog.dom.header = $(".dmui-dialog-header", dialog.dom.root);
    dialog.dom.body = $(".dmui-dialog-body", dialog.dom.root);
    dialog.dom.footer = $(".dmui-dialog-footer", dialog.dom.root);
};
dialog.restDom = function() {
    if (dialog.json.header) {
        dialog.dom.header.show();
        dialog.dom.body.addClass("dmui-dialog-hasHeader");
    } else {
        dialog.dom.header.hide();
        dialog.dom.body.removeClass("dmui-dialog-hasHeader");
    }
    if (dialog.json.footer) {
        dialog.dom.footer.show();
        dialog.dom.body.addClass("dmui-dialog-hasFooter");
    } else {
        dialog.dom.footer.hide();
        dialog.dom.body.removeClass("dmui-dialog-hasFooter");
    }
    dialog.json.width && $(".dmui-dialog-box", dialog.dom.root).css({"width": dialog.json.width});
    dialog.json.zIndex && dialog.dom.root.css({"z-index": dialog.json.zIndex});
    dialog.json.opacity && dialog.dom.root.css({"background": "transparent"});
    $(".dmui-dialog-box", dialog.dom.root).css("margin-top", window.innerHeight / 10 + ($(".dmui-dialog.show").size() - 1) * 20 + "px");
};
dialog.loadSetDom = function() {
    dialog.dom.header.html(dialog.json.header);
    dialog.dom.body.html(dialog.json.body);
    dialog.dom.footer.html(dialog.json.footer);
};
dialog.edit = function() {
    dialog.readDom();
    dialog.restDom();
    dialog.loadSetDom();
    dialog.show();
    dialog.event();
};
dialog.show = function() {
    dialog.dom.root.removeClass("hide").addClass("show");
    $("html").css({});
};
dialog.hide = function(el) {
    [$(el), $(el).parents(".dmui-dialog")].forEach(function($el) {
        $el.addClass("hide").removeClass("show");
    });
    $("html").css({overflow: "unset"});
};
dialog.event = function() {
    var events = [];
    if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
        events = ["touchend"];
    } else {
        events = ["click"];
    }
    dialog.dom.root.off(events.join(",")).on(events.join(","), function(e) {
        $(e.target).attr("data-close") && dialog.hide(e.target);
        e.stopPropagation();
        return false;
    });
    dialog.json.callback && dialog.json.callback();
};
dialog.init = function() {
    var json = arguments[0] !== (void 0) ? arguments[0] : {};
    dialog.json = dialog.default(json);
    $("#J_" + dialog.json.id)[0] ? dialog.edit() : dialog.add();
};
dialog.alert = function(string, callback) {
    dialog.init({
        id: "dmui-dialog-alert",
        width: 120,
        zIndex: 9000,
        opacity: true,
        body: ["<div data-close='' class='text-center dmui-dialog-alert'><i class='glyphicon glyphicon-info-sign rel' style='top: 2px; margin-right: 1rem;width:14px;height:14px;'></i>", string, "</div>"].join(""),
        callback: function() {
            $("#J_dmui-dialog-alert").css({
                "transform": "translate3d(0px, 0px, 0px)",
                "-webkit-transform": "translate3d(0px, 0%, 0px)"
            });
            setTimeout(function() {
                $("#J_dmui-dialog-alert").css({
                    "transform": "translate3d(0px, -100%, 0px)",
                    "-webkit-transform": "translate3d(0px, -100%, 0px)"
                });
                setTimeout(function() {
                    callback && callback();
                }, 300);
            }, 1800);
        }
    });
    $(".dmui-dialog-box", dialog.dom.root).css({
        "width": "100%",
        "margin-top": "0"
    });
};
dialog.confirm = function(string, sureText, callback) {
    dialog.init({
        id: "dmui-dialog-confirm",
        width: 400,
        zIndex: 9000,
        header: "<h4 class='text-center confirm-title'>温馨提示</h4>",
        footer: "<div class='confirm-footer'><span data-close='true'>取消</span><span data-close='true' class='J_submit'>" + sureText + "</span></div>",
        body: ['<div class="dmui-dialog-confirm text-center">', string, '</div>'].join(""),
        callback: function() {
            if (callback) {
                var events = [];
                if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
                    events = ["touchend"];
                } else {
                    events = ["click"];
                }
                $("#J_dmui-dialog-confirm .J_submit").off(events.join(",")).on(events.join(","), callback);
            }
        }
    });
    $(".dmui-dialog-box", dialog.dom.root).css("margin-top", window.innerHeight / 4 + "px");
};
dialog.loading = new Object();
dialog.loading.creat = function() {
    return dialog.init({
        id: "dmui-dialog-loading",
        zIndex: 8999,
        body: ""
    });
};
dialog.loading.show = function() {
    return ($("#J_dmui-dialog-loading")[0] ? $("#J_dmui-dialog-loading").removeClass("hide").addClass("show") : dialog.loading.creat());
};
dialog.loading.hide = function() {
    return $("#J_dmui-dialog-loading").removeClass("show").addClass("hide");
};


/*
* 用法：
dialog.confirm("确定要登录吗", "登录", function(){
    dialog.alert("用户名或密码错误")
});
*
dialog.alert("用户名或密码错误")
*
dialog.init({
     id: "",              //区分模态框的关键值
     width: "",          //宽度
     height: "",         //高度，目前不开放这个属性，使其自动填充
     zIndex: "",         //自定义层级（普通弹窗：1000 - 9000 ，提示框：9000）
     header: "",         //标题栏
     body: "",           //静态内容传入
     ajax: "",           //远程数据预加载
     footer: "",         //页脚内容
     opacity: false,     //背景层是否透明
     callback: null,     //自定义回调
})
*/
